<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="xbsj-labels" content="Earth示例">
    </meta>
    <title>视图-指北针</title>
    <!-- 0 引入js文件：XbsjEarth.js和vue.min.js -->
    <script src="../../XbsjEarth/XbsjEarth.js"></script>
    <script src="./scripts/vue.min.js"></script>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }

        .defultbtn {
            display: inline-block;
            text-align: center;
            min-width: 60px;
            height: 38px;
            padding: 0 10px;
            line-height: 38px;
            border-radius: 100px;
            border: 1px solid #C9C9C9;
            background-color: #FFF;
            color: #555;
            cursor: pointer;
        }

        .defultbtn:hover {
            background-color: #b3daf8;
        }

        .btnon {
            background-color: #1E9FFF;
            color: #fff;
            border: 1px solid #1E9FFF;
        }

        input {
            padding: 5px;
            width: 60px;
        }
    </style>
</head>

<body>
    <div id="vueApp" style="width: 100%; height: 100%; background: grey; position: relative;">
        <earth-comp></earth-comp>
    </div>

    <script>
        // 1 创建Earth的vue组件
        var EarthComp = {
            template: `
                <div style="width: 100%; height: 100%">
                    <div ref="earthContainer" style="width: 100%; height: 100%">
                    </div>
                    <div class="box" style="position: absolute; left: 18px; top: 18px; color: white; background: rgba(0, 0, 0, 0.6); padding: 20px; border-radius: 25px;min-width:200px; font-size:24px; font-family: 宋体;">
                        <div class="defultbtn" :class="{'btnon':showCompass}" @click="showCompass = !showCompass">指北针</div>
                        <div class="defultbtn" :class="{'btnon':showDistanceLegend}" @click="showDistanceLegend = !showDistanceLegend">比例尺</div><br/>
                        <span>指北针位置</span><br/>
                        <span style="vertical-align: middle;">顶部距离：</span><input type="number" v-model="compasstop" @input="compassbottom=undefined"><span style="vertical-align: middle;">右边距离：</span><input type="number" v-model="compassright" @input="compassleft=undefined"><br/>
                        <span style="vertical-align: middle;">底部距离：</span><input type="number" v-model="compassbottom" @input="compasstop=undefined"><span style="vertical-align: middle;">左边距离：</span><input type="number" v-model="compassleft" @input="compassright=undefined"><br/>
                        <span>比例尺位置</span><br/>
                        <span style="vertical-align: middle;">顶部距离：</span><input type="number" v-model="distancetop" @input="distancebottom=undefined"><span style="vertical-align: middle;">右边距离：</span><input type="number" v-model="distanceright" @input="distanceleft=undefined"><br/>
                        <span style="vertical-align: middle;">底部距离：</span><input type="number" v-model="distancebottom" @input="distancetop=undefined"><span style="vertical-align: middle;">左边距离：</span><input type="number" v-model="distanceleft" @input="distanceright=undefined"><br/>
                    </div>
                </div>
            `,
            data() {
                return {
                    _earth: undefined, // 注意：Earth和Cesium的相关变量放在vue中，必须使用下划线作为前缀！
                    showCompass: false, // 控制指北针的开启与关闭，默认为开启
                    showDistanceLegend: false, // 控制比例尺的开启与关闭，默认为开启
                    compasstop: undefined,
                    compassright: undefined,
                    compassbottom: undefined,
                    compassleft: undefined,
                    distancetop: undefined,
                    distanceright: undefined,
                    distancebottom: undefined,
                    distanceleft: undefined
                };
            },
            // 1.1 资源创建
            mounted() {
                // 1.1.1 创建地球
                var earth = new XE.Earth(this.$refs.earthContainer);

                earth.camera.navigator.showCompass = true; // 显示指北针
                earth.camera.navigator.showDistanceLegend = true; // 显示比例尺                

                // 1.1.2 场景配置
                earth.sceneTree.root = {
                    "expand": true,
                    "title": "预览场景",
                    "children": [{
                        "ref": 'tileset',
                        "czmObject": {
                            "name": '大雁塔',
                            "xbsjType": "Tileset",
                            "url": '../assets/dayanta/tileset.json',
                            "xbsjUseOriginTransform": false,
                            "skipLevelOfDetail": false
                        },
                    }, {
                        "czmObject": {
                            "name": '默认影像',
                            "xbsjType": "Imagery",
                            "xbsjImageryProvider": XE.Obj.Imagery.defaultImageryProviderConfig,
                        },
                    }]
                };

                var tileset = earth.sceneTree.$refs.tileset.czmObject;

                // 飞入大雁塔
                tileset.flyTo();

                // 1.1.3 数据绑定
                // 双向绑定的属性，如果一方发生变化，另一方同样会跟随变化
                this._showCompassUnbind = XE.MVVM.bind(this, 'showCompass', earth.camera.navigator, 'showCompass');
                this._topCompassUnbind = XE.MVVM.bind(this, 'compasstop', earth.camera.navigator.compassStyle, 'top');
                this._rightCompassUnbind = XE.MVVM.bind(this, 'compassright', earth.camera.navigator.compassStyle, 'right');
                this._bottomCompassUnbind = XE.MVVM.bind(this, 'compassbottom', earth.camera.navigator.compassStyle, 'bottom');
                this._leftCompassUnbind = XE.MVVM.bind(this, 'compassleft', earth.camera.navigator.compassStyle, 'left');
                this._showDistanceLegendUnbind = XE.MVVM.bind(this, 'showDistanceLegend', earth.camera.navigator, 'showDistanceLegend');
                this._topDistanceUnbind = XE.MVVM.bind(this, 'distancetop', earth.camera.navigator.distanceLegendStyle, 'top');
                this._rightDistanceUnbind = XE.MVVM.bind(this, 'distanceright', earth.camera.navigator.distanceLegendStyle, 'right');
                this._bottomDistanceUnbind = XE.MVVM.bind(this, 'distancebottom', earth.camera.navigator.distanceLegendStyle, 'bottom');
                this._leftDistanceUnbind = XE.MVVM.bind(this, 'distanceleft', earth.camera.navigator.distanceLegendStyle, 'left');

                this._earth = earth;

                // only for Debug
                window.tileset = tileset;
                window.earth = earth;
            },
            filters: {
                numFilter(value) {
                    // 截取当前数据到小数点后两位
                    let realVal = parseFloat(value).toFixed(2)
                    return realVal
                }
            },
            // 1.2 资源销毁
            beforeDestroy() {
                // vue程序销毁时，需要清理相关资源
                this._showCompassUnbind = this._showCompassUnbind && this._showCompassUnbind();
                this._topCompassUnbind = this._topCompassUnbind && this._topCompassUnbind();
                this._rightCompassUnbind = this._rightCompassUnbind && this._rightCompassUnbind();
                this._bottomCompassUnbind = this._bottomCompassUnbind && this._bottomCompassUnbind();
                this._leftCompassUnbind = this._leftCompassUnbind && this._leftCompassUnbind();
                this._showDistanceLegendUnbind = this._showDistanceLegendUnbind && this._showDistanceLegendUnbind();
                this._topDistanceUnbind = this._topDistanceUnbind && this._topDistanceUnbind();
                this._rightDistanceUnbind = this._rightDistanceUnbind && this._rightDistanceUnbind();
                this._bottomDistanceUnbind = this._bottomDistanceUnbind && this._bottomDistanceUnbind();
                this._leftDistanceUnbind = this._leftDistanceUnbind && this._leftDistanceUnbind();
                this._earth = this._earth && this._earth.destroy();
            },
        }

        // 2 创建vue程序
        // XE.ready()用来加载Cesium.js等相关资源
        XE.ready().then(() => {
            var app = new Vue({
                el: '#vueApp',
                components: {
                    EarthComp,
                },
            });
        });
    </script>
</body>

</html>